//添加官方帖子
const router = require('koa-router')()
const fs = require("fs");
const path = require("path");
const uploadDir = path.join(__dirname, '../images/guide');

router.prefix('/tips')

//backend
//页面加载完成时获取数据库里全部数据
router.post('/gettipsbackend', async (ctx, next) => {
    let result = await ctx.db.EXCUTE('SELECT * FROM guide ORDER BY guideid');

    ctx.body = {
        code: 200,
        data: result
    }
})

//添加新项
router.post('/addtipsofficialbackend', async (ctx, next) => {
    let title = ctx.request.body.title;
    let content = ctx.request.body.content;
    let coverimgbig = ctx.request.body.coverimgbig;
    let innerimg = ctx.request.body.innerimg;
    let coverimgsmall = ctx.request.body.coverimgsmall;
    let isrotograph = ctx.request.body.isrotograph;
    let classification = ctx.request.body.classification;
    let date = ctx.request.body.date;

    console.log(coverimgbig);

    var base64Data = coverimgbig.url.replace(/^data:image\/\w+;base64,/, "");
    var dataBuffer = Buffer.from(base64Data, 'base64');
    let imgUrl = uploadDir + '\\' + coverimgbig.name
    fs.writeFileSync(imgUrl, dataBuffer)

    var base64Data = coverimgsmall.url.replace(/^data:image\/\w+;base64,/, "");
    var dataBuffer = Buffer.from(base64Data, 'base64');
    let imgUrlcoverimgsmall = uploadDir + '\\' + coverimgsmall.name
    fs.writeFileSync(imgUrlcoverimgsmall, dataBuffer)

    var base64Data = innerimg.url.replace(/^data:image\/\w+;base64,/, "");
    var dataBuffer = Buffer.from(base64Data, 'base64');
    let imgUrlinnerimg = uploadDir + '\\' + innerimg.name
    fs.writeFileSync(imgUrlinnerimg, dataBuffer)

    let sql0 = 'INSERT INTO guide (title,content,coverimgbig,innerimg,coverimgsmall,isrotograph,classification,date,readnum) VALUES (?,?,?,?,?,?,?,?,0)'
    let result1 = await ctx.db.EXCUTE(sql0, [title, content, '/images/guide/' + coverimgbig.name, '/images/guide/' + innerimg.name, '/images/guide/' + coverimgsmall.name, isrotograph, classification, date]);

    let result = await ctx.db.EXCUTE('SELECT * FROM guide ORDER BY guideid');

    ctx.body = {
        code: 200,
        data: result
    }
})

//编辑item 首先获取该项的内容
router.post('/getsingletipsbackend', async (ctx, next) => {
    let guideid = ctx.request.body.guideid;

    let sql = 'SELECT * FROM guide WHERE guideid=?';
    let result = await ctx.db.EXCUTE(sql, [guideid]);

    ctx.body = {
        code: 200,
        data: result
    }
})

//编辑项
router.post('/modifytipsbackend', async (ctx, next) => {
    let guideid = ctx.request.body.guideid;
    let title = ctx.request.body.title;
    let content = ctx.request.body.content;
    let isrotograph = ctx.request.body.isrotograph;
    let classification = ctx.request.body.classification;
    let date = ctx.request.body.date;

    let sql = 'UPDATE guide SET title=?, content=?, isrotograph=?, classification=?, date=?, readnum=0 WHERE guideid=?'
    let result1 = await ctx.db.EXCUTE(sql, [title, content, isrotograph, classification, date, guideid]);

    let result = await ctx.db.EXCUTE('SELECT * FROM guide ORDER BY guideid');

    ctx.body = {
        code: 200,
        data: result
    }

});

//删除项
router.post('/deletetipsbackend', async (ctx, next) => {
    let guideid = ctx.request.body.guideid;

    let result0 = await ctx.db.EXCUTE('select * from guide where guideid = ?', [guideid]);
    let arr = [result0[0].coverimgbig, result0[0].coverimgsmall, result0[0].innerimg]
    for (let i = 0; i < arr.length; i++) {
        console.log(arr[i]);
        fs.unlink(path.join(__dirname, '../' + arr[i]), async function (err) {
            if (err) {
                console.log(err);
                ctx.body = {
                    code: 400
                }
            }
        })
    }
    let sql = 'DELETE FROM guide WHERE guideid=?'
    let result1 = await ctx.db.EXCUTE(sql, [guideid]);
    let result = await ctx.db.EXCUTE('SELECT * FROM guide ORDER BY guideid');

    ctx.body = {
        code: 200,
        data: result
    }

});

module.exports = router